package com.css.zfzx.fwsjcj.modules.expert.respository.impl;

import com.css.zfzx.fwsjcj.modules.expert.entity.FwUserEntity;
import com.css.zfzx.fwsjcj.modules.expert.respository.FwExpertQueryRespository;
import com.css.zfzx.fwsjcj.modules.expert.vo.ExpertVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2021/12/22 9:35
 **/
@Repository
public class FwExpertQueryRespositoryImpl implements FwExpertQueryRespository {
    @PersistenceContext
    EntityManager entityManager;

    @Override
    public Page<FwUserEntity> getPage(HttpServletRequest request, int curPage, int pageSize, ExpertVo expertVo) {
        StringBuilder sql=new StringBuilder("select a.*,b.* from  " +
                "(select org_user.user_id,org_user.user_name,org_user.user_code,org_role_user.is_valid  from org_role_user,org_user,org_role " +
                "where org_user.user_id=org_role_user.user_id and org_role.role_id=org_role_user.role_id and org_role.role_code=017 and org_role_user.is_valid=1)" +
                " a left join fw_expert_direction b on a.user_id=b.user_id ");
        StringBuilder whereSql=new StringBuilder("where a.is_valid=1");
        StringBuilder countSql =new StringBuilder("select count(*) from  " +
                "(select org_user.user_id,org_user.user_name,org_user.user_code,org_role_user.is_valid  from org_role_user,org_user,org_role " +
                "where org_user.user_id=org_role_user.user_id and org_role.role_id=org_role_user.role_id and org_role.role_code=017 and org_role_user.is_valid=1) " +
                "a left join fw_expert_direction b on a.user_id=b.user_id ");
        if(!StringUtils.isEmpty(expertVo.getDeptId())){
            sql.delete(0,sql.length());
            sql.append("select a.*,b.* from  " +
                    "(select org_user.user_id,org_user.user_name,org_user.user_code,org_role_user.is_valid  from org_role_user,org_user,org_role,org_dept_user " +
                    "where org_user.user_id=org_role_user.user_id and org_role.role_id=org_role_user.role_id and org_role.role_code=017 " +
                    " and org_role_user.is_valid=1 " +
                    "and org_user.user_id=org_dept_user.user_id and org_dept_user.dept_id=:deptId)" +
                    " a left join fw_expert_direction b on a.user_id=b.user_id ");
            countSql.delete(0,sql.length());
            countSql.append("select count(*) from  " +
                    "(select org_user.user_id,org_user.user_name,org_user.user_code,org_role_user.is_valid  from org_role_user,org_user,org_role,org_dept_user " +
                    "where org_user.user_id=org_role_user.user_id and org_role.role_id=org_role_user.role_id and org_role.role_code=017 " +
                            " and org_role_user.is_valid=1 " +
                    "and org_user.user_id=org_dept_user.user_id and org_dept_user.dept_id=:deptId)" +
                    " a left join fw_expert_direction b on a.user_id=b.user_id ");
        }
        if(!StringUtils.isEmpty(expertVo.getUserName())){
            whereSql.append(" and user_name like :userName");
        }
        if(!StringUtils.isEmpty(expertVo.getUserCode())){
            whereSql.append(" and user_code like :userCode");
        }
        if(StringUtils.isEmpty(expertVo.getUserName()) && StringUtils.isEmpty(expertVo.getUserCode())){
            whereSql.delete(0,whereSql.length());
        }
        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).toString(), FwUserEntity.class);

        Query countQuery = entityManager.createNativeQuery(countSql.append(whereSql).toString());
        if(!StringUtils.isEmpty(expertVo.getUserName())){
            nativeQuery.setParameter("userName","%"+ expertVo.getUserName()+"%");
            countQuery.setParameter("userName","%"+ expertVo.getUserName()+"%");
        }
        if(!StringUtils.isEmpty(expertVo.getUserCode())) {

            nativeQuery.setParameter("userCode", "%" + expertVo.getUserCode() + "%");
            countQuery.setParameter("userCode", "%" + expertVo.getUserCode() + "%");
        }
        if(!StringUtils.isEmpty(expertVo.getDeptId())) {
            nativeQuery.setParameter("deptId", expertVo.getDeptId() );
            countQuery.setParameter("deptId", expertVo.getDeptId());
        }
        Pageable pageable= PageRequest.of(curPage-1,pageSize);
        nativeQuery.setFirstResult(pageable.getPageNumber()*pageable.getPageSize());
        nativeQuery.setMaxResults(pageable.getPageSize());
        List<FwUserEntity> list=nativeQuery.getResultList();
        /*String structureType="";
        for(FwUserEntity fUserEntity:list){
            if(fUserEntity.getStructureType()!=null){
                String[] arr = fUserEntity.getStructureType().split(",");
                for(int a=0;a<arr.length;a++){
                    if(arr[a].equals("0")){
                        structureType+="砖混结构、砖木结构，";
                    }else if(arr[a].equals("1")){
                        structureType+="多层钢筋混凝土结构，";
                    }else if(arr[a].equals("2")){
                        structureType+="高层结构，";
                    }else if(arr[a].equals("3")){
                        structureType+="工业厂房结构，";
                    }else if(arr[a].equals("4")){
                        structureType+="空旷结构，";
                    }else if(arr[a].equals("5")){
                        structureType+="土木结构，";
                    }else if(arr[a].equals("6")){
                        structureType+="木结构，";
                    }else if(arr[a].equals("7")){
                        structureType+="石结构，";
                    }else if(arr[a].equals("8")){
                        structureType+="其它，";
                    }
                }
                fUserEntity.setStructureType(structureType.substring(0, structureType.length() - 1));
            }
        }*/
        BigInteger count =(BigInteger) countQuery.getSingleResult();
        Page<FwUserEntity> page=new PageImpl<>(list,pageable,count.longValue());
        return page;
    }
}
